package com.toivan.demo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.toivan.demo.databinding.ActivityPictureBinding;
import com.toivan.mt.views.MtBeautyPanel;
import com.toivan.sdk.MtSDK;
import com.toivan.sdk.model.MtPixelsFormat;
import com.toivan.sdk.model.MtRotation;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

public class PictureActivity extends AppCompatActivity {

    private static final int READ_REQUEST_CODE = 20;
    private ActivityPictureBinding binding;
    private ImageView picture;
    private Button album, render;
    private int pictureWidth, pictureHeight;
    private byte[] pictureByte;
    private boolean isRenderInit;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityPictureBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        picture = binding.picture;
        album = binding.album;
        render = binding.render;

        addContentView(new MtBeautyPanel(this).init(MtSDK.get()),
                new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        album.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");
                startActivityForResult(intent, READ_REQUEST_CODE);
            }
        });

        render.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                renderPicture();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            if (data == null || data.getData() == null) {
                return;
            }
            Uri uri = data.getData();
            try {
                InputStream is = getContentResolver().openInputStream(uri);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inDither = false;
                options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                Bitmap bitmap = BitmapFactory.decodeStream(is, null, options);
                if (bitmap == null) {
                    return;
                }
                pictureWidth = bitmap.getWidth();
                pictureHeight = bitmap.getHeight();
                ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount());
                bitmap.copyPixelsToBuffer(buffer);
                pictureByte = buffer.array();

                picture.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void renderPicture() {
        if (pictureByte != null) {
            byte[] renderBytes = new byte[pictureByte.length];
            System.arraycopy(pictureByte, 0, renderBytes, 0, pictureByte.length);

            if (!isRenderInit) {
                isRenderInit = MtSDK.get().initRenderPixels(MtPixelsFormat.RGBA, pictureWidth, pictureHeight, MtRotation.CLOCKWISE_0, false, 5);
            }
            MtSDK.get().renderPixels(renderBytes);

            Bitmap renderedBm = Bitmap.createBitmap(pictureWidth, pictureHeight, Bitmap.Config.ARGB_8888);
            renderedBm.copyPixelsFromBuffer(ByteBuffer.wrap(renderBytes));
            picture.setImageBitmap(renderedBm);
        }
    }
}
